package trong.gold.soft.com.view.game;

import trong.gold.soft.com.main.*;
import trong.gold.soft.com.model.Resources;
import trong.gold.soft.com.model.font.VietnameseFont;
import trong.gold.soft.com.view.menuSlGame;

import javax.microedition.lcdui.Graphics;

import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

public class startGame extends GameCanvas implements Runnable{
	private LayerManager lm;
	 private Sprite actor,trangthai;
	 private int ttin=-180;
	 private int trthai = -252;
	 private boolean run=true;
	 private String req="Ấn phím bất kỳ";
	 private Graphics grap;
	 private mTetrisMidlet midlet;
	 
	public startGame(mTetrisMidlet midlet) {
		super(false);
		this.midlet = midlet;
		setFullScreenMode(true);
		lm = new LayerManager();
		this.grap = this.getGraphics();
		actor = new Sprite(Resources.imgactor, 180, 252);
		trangthai = new Sprite(Resources.imgtrthai, 60, 252);
		sprite(actor,ttin,true);
		sprite(trangthai,trthai,false);
		Thread t=new Thread(this);
	    t.start();
	}
	private void sprite(Sprite s,int x,boolean t){
		 s.setFrame(0);
		 if(t)
			 s.setPosition(x, 0);
		 else
			 s.setPosition(180, x);
	     lm.append(s);
	}
	private void drawkhung(Graphics g){
		for(int i=0;i<=this.getWidth()/44;i++){
			g.drawImage(Resources.backgr, i*Resources.backgr.getWidth(), 0, Resources.TOP_LEFT_ANCHOR);
		}
		g.drawImage(Resources.imgicon, this.getWidth()/2 - Resources.imgicon.getWidth()/2, 10, Resources.TOP_LEFT_ANCHOR);
		actor.setPosition(ttin, 0);
		trangthai.setPosition(180, trthai);
		lm.paint(g, 0, 0);
        flushGraphics();
	}
	private void thongbao(Graphics g){
		g.drawImage(Resources.imgstart, this.getWidth()/2 - 60, this.getHeight()/4, Resources.TOP_LEFT_ANCHOR);
		VietnameseFont.BlackBold.DrawString(g, req ,this.getWidth()/2, this.getHeight() - VietnameseFont.WhiteBold.getHeight(), Graphics.HCENTER);
	}
	public void run() {
		while(run){
			Graphics g = getGraphics();
			if(ttin>-10)
				ttin=0;
			else
				ttin+=10;
			if(trthai>-10)
				trthai=0;
			else
				trthai+=10;
			if(ttin==0 && trthai==0){
				thongbao(grap);
				flushGraphics();
				run=false;
				break;
			}
			drawkhung(g);
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
			}
		}
	}
	private void switchState(int x){
		if(x==0){
			midlet.switchState(this, new TetrisClassic(midlet));
			return;
		}
		if(x==1){
			midlet.switchState(this, new HotGirl(midlet));
			return;
		}
		if(x==2){
			midlet.switchState(this, new HienDai(midlet));
			return;
		}
	}
	public void keyPressed(int keyCode) {
		if(!run){
			setnull();
			Runtime.getRuntime().gc();
			switchState(menuSlGame.getkt());
				
		}
		
	}
	public void pointerPressed(int x,int y){
		if(!run){
			setnull();
			Runtime.getRuntime().gc();
			switchState(menuSlGame.getkt());
		}
	}
	private void setnull(){
		lm=null;
		actor=null;
		trangthai=null;
		req=null;
		grap=null;
	}

}
